package com.hy.service.impl;

import com.hy.bean.Goods;
import com.hy.dao.GoodsDao;
import com.hy.dao.impl.GoodsDaoImpl;
import com.hy.service.GoodsService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * (Goods)表服务实现类
 *
 * @author makejava
 * @since 2024-04-29 11:08:29
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

    private GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public List<Goods> queryAll() {
        return goodsDao.queryByName();
    }


    /**
     * 新增数据
     *
     * @param goods 实例对象
     * @return 实例对象
     */
    @Override
    public Goods insert(Goods goods) throws Exception {
        this.goodsDao.insert(goods);
        return goods;
    }

    /**
     * 修改数据
     *
     * @param goods 实例对象
     * @return 实例对象
     */
    @Override
    public Goods update(Goods goods) throws Exception {
        this.goodsDao.update(goods);
        return goods;
    }

    /**
     * 通过主键删除数据
     *
     * @param id
     * @return 是否成功
     */
    @Override
    public boolean deleteById(int id) throws Exception {
        BigDecimal count = goodsDao.count(id);
        if (count.compareTo(BigDecimal.ZERO) == 0) {
            goodsDao.deleteById(id);
        }
        return count.compareTo(BigDecimal.ZERO) > 0;
    }

    @Override
    public List<Map<String, Object>> selectAll(String name) throws Exception {
        if(name==null || name.trim()==""){
            name="";
        }
        return goodsDao.selectAll(name);
    }
}
